<template>
  <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    status-icon
    :rules="rules"
    label-width="120px"
    class="demo-ruleForm"
  >
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="ruleForm.email" clearable type="email"></el-input>
      <el-button
        type="warning"
        :loading="checkCodeBtn.loading"
        :disabled="checkCodeBtn.disabled"
        @click="getCheckCode"
        style="margin-left: 5px"
      >{{ checkCodeBtn.text }}</el-button>
    </el-form-item>
    <el-form-item label="验证码" prop="passWord">
      <el-input v-model="ruleForm.code" type="password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="新的密码" prop="passWord">
      <el-input v-model="ruleForm.passWord" type="password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">Submit</el-button>
      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { ref, reactive,h } from "vue";
import type { ElForm  } from "element-plus";
import { ElNotification } from 'element-plus'
import axios from "axios"
import {useRoute} from "vue-router"
type FormInstance = InstanceType<typeof ElForm>;
const ruleFormRef = ref<FormInstance>();
let checkCodeBtn = reactive<any>({
  text: '获取验证码',
  loading: false,
  disabled: false,
  duration: 60,
  timer: null
})
const route=useRoute();
const getCheckCode = () => {
  console.log(route.query);
  const userName=route.query.userName;
  const email=ruleForm.email;
  const passWord=ruleForm.passWord;
  axios.get('http://localhost',{
    params:{
      email,passWord,userName
    }
  })
  // 倒计时期间按钮不能单击
  if (checkCodeBtn.duration !== 60) {
    checkCodeBtn.disabled = true
  }
  // 清除掉定时器
  checkCodeBtn.timer && clearInterval(checkCodeBtn.timer);
  axios.get('http://localhost:8081')
  .then((res) => {
    alert(res)
  }).catch((error) => {
    alert(error)
  })
  // 开启定时器
  checkCodeBtn.timer = setInterval(() => {
    const tmp = checkCodeBtn.duration--
    checkCodeBtn.text = `${tmp}秒`
    if (tmp <= 0) {
      // 清除掉定时器
      clearInterval(checkCodeBtn.timer)
      checkCodeBtn.duration = 60
      checkCodeBtn.text = '重新获取'
      // 设置按钮可以单击
      checkCodeBtn.disabled = false
    }
  }, 1000)
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  }
  else if (value.length < 8 || value.length > 16) {
    callback(new Error("密码要在8-16位之间"));
  }
  else {
    if (ruleForm.checkPass !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass", () => null);
    }
    callback();
  }
};
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请再次输入密码"));
  } else if (value !== ruleForm.passWord) {
    callback(new Error("密码不匹配"));
  } else {
    callback();
  }
};

const ruleForm = reactive({
  passWord: "",
  checkPass: "",
  code: "",
  email: "",
});

const rules = reactive({
  code: [{
    required: true,
    message: '请输入验证码',
    trigger: 'blur',
  }, {
    min: 6,
    max: 6,
    message: '验证码需要6位',
    trigger: 'blur',
  }],
  passWord: [{ required: true, validator: validatePass, trigger: "blur" }],
  checkPass: [{ required: true, validator: validatePass2, trigger: "blur" }],
  email: [{
    required: true,
    message: '请输入邮箱',
    trigger: 'blur',
  }, {
    pattern: 'w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*',
    message: '请输入正确的邮箱格式',
    trigger: 'blur',
  }
  ]
});

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      alert("submit!");
      axios.get('http://localhost:8081/', {
        params:{
          passWord:ruleForm.passWord,
          userName:route.query.userName,
          email:ruleForm.email
        }
      }).then((res)=>{
        console.log(res)
      }).catch((error)=>{
        console.log(error);
      })
    } else {
      ElNotification({
        title: '格式错误',
        message: h('text', { style: 'color: red' }, '信息填写格式错误'),
        type: 'error'
      })
      return false;
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>
<style scoped>
.demo-ruleForm {
  width: 100%;
}
</style>